ggplot
plotlygganimateggplot
facet_*trelliscopeJSggplotgapminderIdentify the target variable from gapminder dataset.
Select one country and visualize lifeExp trend over year.
gapminder %>%
filter(str_detect(country, "Korea, Rep")) %>%
ggplot(aes(x=year, y=lifeExp)) +
geom_line() +
geom_point()Compare lifeExp with gdpPercap over year.
gapminder %>%
filter(str_detect(country, "Korea, Rep")) %>%
select(year, lifeExp, gdpPercap) %>%
gather(variable, value, -year) %>%
ggplot(aes(x=year, y=value)) +
geom_line() +
geom_point() +
facet_wrap(~variable, scale="free")Decorate through adding labels, title, and applying a theme.
library(extrafont)
loadfonts()
gapminder %>%
filter(str_detect(country, "Korea, Rep")) %>%
select(year, lifeExp, gdpPercap) %>%
gather(variable, value, -year) %>%
ggplot(aes(x=year, y=value)) +
geom_line() +
geom_point() +
facet_wrap(~variable, scale="free") +
labs(x="", y="",
title="GDP per capita and Life Expectancy over time",
subtitle="Data from gapminder, Korea") +
theme_bw(base_family = "NanumGothic")plotlylife_p <- gapminder %>%
ggplot(aes(x=year, y=lifeExp, group=country)) +
geom_line()
library(plotly)
ggplotly(life_p)tooltiplifeExp_highlight <- highlight_key(gapminder, ~country)
highlight_p <- lifeExp_highlight %>%
ggplot(aes(x=year, y=lifeExp,
group=country,
text=paste0("Continent: ", continent, "\n",
"Country: ", country, "\n",
"Life Expectancy: ", lifeExp))) +
geom_line()
highlight_gg <- ggplotly(highlight_p, tooltip = "text")
highlight(highlight_gg, on = "plotly_click",
selectize = TRUE,
dynamic = TRUE,
persistent = TRUE)life_g <- gapminder %>%
ggplot(aes(x=gdpPercap, y=lifeExp, group=country, color=country,
text=paste0("Continent: ", continent, "\n",
"Country: ", country, "\n",
"Life Expectancy: ", lifeExp))) +
geom_point(aes(frame=year), color="red") +
geom_point(alpha=0.2) +
facet_wrap(~continent, ncol=2) +
scale_x_sqrt() +
theme(legend.position = "none")
ggplotly(life_g, tooltip = "text")gganimate transition_time()library(gganimate)
gapminder %>%
ggplot(aes(x=gdpPercap, y=lifeExp, group=country, color=country,
text=paste0("Continent: ", continent, "\n",
"Country: ", country, "\n",
"Life Expectancy: ", lifeExp))) +
geom_point(aes(size=gdpPercap)) +
facet_wrap(~continent, ncol=2) +
scale_x_sqrt() +
theme(legend.position = "none") +
transition_time(year) +
labs(title = "Year: {frame_time}")